/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CalculadoraJuros.java
 *
 * Created on 15/08/2011, 11:34:56
 */

package calculadorafinanceira;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


/**
 *
 * @author COMPRAS
 */
public class CalculadoraJuros extends javax.swing.JFrame {

    /** Creates new form CalculadoraJuros */
    public CalculadoraJuros() {
        initComponents();

        //Alterando o Icone na barra de titulo
        URL url = this.getClass().getResource("icons/Calculadora1.png");
        Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(imagemTitulo);


        //Método utilizado para centralizar o JFrame na tela
        Dimension tela = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((tela.width - this.getSize().width) / 2, (tela.height - this.getSize().height) / 2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        rdbVlrInicial = new javax.swing.JRadioButton();
        rdbVlrParcelas = new javax.swing.JRadioButton();
        rdbTxJuros = new javax.swing.JRadioButton();
        rdbMeses = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        btnCalcular = new javax.swing.JButton();
        ftxtVlrParcelas = new javax.swing.JFormattedTextField();
        ftxtVlrInicial = new javax.swing.JFormattedTextField();
        ftxtTxJuros = new javax.swing.JFormattedTextField();
        ftxtMeses = new javax.swing.JFormattedTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(CalculadoraJuros.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        buttonGroup1.add(rdbVlrInicial);
        rdbVlrInicial.setText(resourceMap.getString("rdbVlrInicial.text")); // NOI18N
        rdbVlrInicial.setName("rdbVlrInicial"); // NOI18N
        rdbVlrInicial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbVlrInicialActionPerformed(evt);
            }
        });

        buttonGroup1.add(rdbVlrParcelas);
        rdbVlrParcelas.setText(resourceMap.getString("rdbVlrParcelas.text")); // NOI18N
        rdbVlrParcelas.setName("rdbVlrParcelas"); // NOI18N
        rdbVlrParcelas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbVlrParcelasActionPerformed(evt);
            }
        });

        buttonGroup1.add(rdbTxJuros);
        rdbTxJuros.setText(resourceMap.getString("rdbTxJuros.text")); // NOI18N
        rdbTxJuros.setName("rdbTxJuros"); // NOI18N
        rdbTxJuros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbTxJurosActionPerformed(evt);
            }
        });

        buttonGroup1.add(rdbMeses);
        rdbMeses.setText(resourceMap.getString("rdbMeses.text")); // NOI18N
        rdbMeses.setName("rdbMeses"); // NOI18N
        rdbMeses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbMesesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(rdbVlrInicial)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdbVlrParcelas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdbTxJuros)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rdbMeses)
                .addContainerGap(71, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rdbVlrInicial)
                    .addComponent(rdbVlrParcelas)
                    .addComponent(rdbTxJuros)
                    .addComponent(rdbMeses))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("jLabel11.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setFont(resourceMap.getFont("jLabel11.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setFont(resourceMap.getFont("jLabel11.font")); // NOI18N
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel11.setFont(resourceMap.getFont("jLabel11.font")); // NOI18N
        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        btnCalcular.setFont(resourceMap.getFont("btnCalcular.font")); // NOI18N
        btnCalcular.setText(resourceMap.getString("btnCalcular.text")); // NOI18N
        btnCalcular.setEnabled(false);
        btnCalcular.setName("btnCalcular"); // NOI18N
        btnCalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalcularActionPerformed(evt);
            }
        });
        btnCalcular.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnCalcularKeyPressed(evt);
            }
        });

        ftxtVlrParcelas.setEditable(false);
        ftxtVlrParcelas.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,###.00"))));
        ftxtVlrParcelas.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ftxtVlrParcelas.setName("ftxtVlrParcelas"); // NOI18N

        ftxtVlrInicial.setEditable(false);
        ftxtVlrInicial.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,###.00"))));
        ftxtVlrInicial.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ftxtVlrInicial.setText(resourceMap.getString("ftxtVlrInicial.text")); // NOI18N
        ftxtVlrInicial.setName("ftxtVlrInicial"); // NOI18N

        ftxtTxJuros.setEditable(false);
        ftxtTxJuros.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.0####"))));
        ftxtTxJuros.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ftxtTxJuros.setText(resourceMap.getString("ftxtTxJuros.text")); // NOI18N
        ftxtTxJuros.setName("ftxtTxJuros"); // NOI18N

        ftxtMeses.setEditable(false);
        ftxtMeses.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        ftxtMeses.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ftxtMeses.setText(resourceMap.getString("ftxtMeses.text")); // NOI18N
        ftxtMeses.setName("ftxtMeses"); // NOI18N
        ftxtMeses.setNextFocusableComponent(btnCalcular);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel6))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)))
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ftxtVlrInicial, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                    .addComponent(ftxtVlrParcelas, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                    .addComponent(ftxtMeses, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                    .addComponent(ftxtTxJuros, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                        .addComponent(btnCalcular))
                    .addComponent(jLabel7))
                .addGap(62, 62, 62))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ftxtVlrInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ftxtVlrParcelas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(ftxtTxJuros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ftxtMeses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel11)
                    .addComponent(btnCalcular))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setMnemonic('a');
        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem1.setMnemonic('s');
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
                        .addGap(4, 4, 4)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rdbVlrInicialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbVlrInicialActionPerformed
        limpar_dados();
        ftxtVlrInicial.setText("0");

        btnCalcular.setEnabled(true);

        selecao = 1;

        ftxtVlrInicial.setEditable(false);
        ftxtVlrParcelas.setEditable(true);
        ftxtTxJuros.setEditable(true);
        ftxtMeses.setEditable(true);
    }//GEN-LAST:event_rdbVlrInicialActionPerformed

    private void rdbVlrParcelasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbVlrParcelasActionPerformed
        limpar_dados();
        ftxtVlrParcelas.setText("0");

        btnCalcular.setEnabled(true);

        selecao = 2;

        ftxtVlrInicial.setEditable(true);
        ftxtVlrParcelas.setEditable(false);
        ftxtTxJuros.setEditable(true);
        ftxtMeses.setEditable(true);
    }//GEN-LAST:event_rdbVlrParcelasActionPerformed

    private void rdbTxJurosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbTxJurosActionPerformed
        limpar_dados();
        ftxtTxJuros.setText("0");

        btnCalcular.setEnabled(true);

        selecao = 3;

        ftxtVlrInicial.setEditable(true);
        ftxtVlrParcelas.setEditable(true);
        ftxtTxJuros.setEditable(false);
        ftxtMeses.setEditable(true);
    }//GEN-LAST:event_rdbTxJurosActionPerformed

    private void rdbMesesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbMesesActionPerformed
        limpar_dados();
        ftxtMeses.setText("0");
        
        btnCalcular.setEnabled(true);

        selecao = 4;

        ftxtVlrInicial.setEditable(true);
        ftxtVlrParcelas.setEditable(true);
        ftxtTxJuros.setEditable(true);
        ftxtMeses.setEditable(false);
    }//GEN-LAST:event_rdbMesesActionPerformed

    private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCalcularActionPerformed
        if(!validaDados())
        {
        }
        else
        {
            switch (selecao)
            {
                case 1:
                    try {
                        calcular_inicial();
                    } catch (ParseException ex) {
                        Logger.getLogger(CalculadoraJuros.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 2:
                    try {
                        calcular_parcelas();
                    } catch (ParseException ex) {
                        Logger.getLogger(CalculadoraJuros.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 3:
                    try {
                        calcular_taxas();
                    } catch (ParseException ex) {
                        Logger.getLogger(CalculadoraJuros.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                case 4:
                    try {
                        calcular_meses();
                    } catch (ParseException ex) {
                        Logger.getLogger(CalculadoraJuros.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "Nenhum item foi indicado");
            }
        }

    }//GEN-LAST:event_btnCalcularActionPerformed

    private void btnCalcularKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnCalcularKeyPressed
        btnCalcularActionPerformed(null);
    }//GEN-LAST:event_btnCalcularKeyPressed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
        
    }//GEN-LAST:event_jMenu1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        if (sobre == null) {
            Sobre frame = new Sobre(this, rootPaneCheckingEnabled);
            frame.setVisible(true);
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CalculadoraJuros().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCalcular;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JFormattedTextField ftxtMeses;
    private javax.swing.JFormattedTextField ftxtTxJuros;
    private javax.swing.JFormattedTextField ftxtVlrInicial;
    private javax.swing.JFormattedTextField ftxtVlrParcelas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton rdbMeses;
    private javax.swing.JRadioButton rdbTxJuros;
    private javax.swing.JRadioButton rdbVlrInicial;
    private javax.swing.JRadioButton rdbVlrParcelas;
    // End of variables declaration//GEN-END:variables

    private Integer selecao;
    private String sinicial;
    private String sparcelas;
    private String staxa;
    private double potencia;
    private double base;
    private double inicial;
    private double parcelas;
    private double taxa;
    private double taxaFinal;
    private int meses;
    private double dmeses;
    private double total;
    private String staxaFinal;
    private Sobre sobre;

    private void calcular_inicial() throws ParseException {
        //buscando os textos das caixas de texto
        staxa = ftxtTxJuros.getText();
        sparcelas = ftxtVlrParcelas.getText();
        meses = Integer.parseInt(ftxtMeses.getText());

        //Substituindo o texto recebido por valor numerico
        taxa = NumberFormat.getInstance().parse(staxa).doubleValue();
        taxa = taxa/100;
        parcelas = NumberFormat.getInstance().parse(sparcelas).doubleValue();

        //efetuando o calculo conforme a formula:
        // inicial = (1-(1+juros)^(-meses))*parcelas/juros
        base = 1 + taxa;
        dmeses = - meses;
        potencia = Math.pow(base, dmeses);
        inicial = (1-potencia) * parcelas/taxa;

        //formatando para 2 casas decimais
        //(para 3 casas utilizaria: inicial*1000.0
        //assim por diante
        inicial = ( Math.round( (inicial * 100.0) ) ) / 100.0;
        sinicial = String.valueOf(inicial);
        
        //substituindo o "." por ","
        sinicial = sinicial.replace(".", ",");

        ftxtVlrInicial.setText(sinicial);
        /*ftxtInvisivel.setVisible(true);
        lblInvisivel.setVisible(true);

        lblInvisivel.setText("Valor Inicial: R$");
        ftxtInvisivel.setText(sinicial);*/
    }

    private void calcular_parcelas() throws ParseException {
        //buscando os textos das caixas de texto
        sinicial = ftxtVlrInicial.getText();
        staxa = ftxtTxJuros.getText();
        meses = Integer.parseInt(ftxtMeses.getText());

        //Substituindo o texto recebido por valor numerico
        inicial = NumberFormat.getInstance().parse(sinicial).doubleValue();
        taxa = NumberFormat.getInstance().parse(staxa).doubleValue();
        taxa = taxa/100;

        //efetuando o calculo conforme a formula:
        // parcelas = inicial * juros/(1-(1+juros)^(-meses))
        base = 1 + taxa;
        dmeses = - meses;
        potencia = Math.pow(base, dmeses);
        parcelas = inicial * taxa / (1-potencia);

        //formatando casas decimais
        // para 2 casas: inicial*100.0
        // para 3 casas: inicial*1000.0 etc
        parcelas = ( Math.round( (parcelas * 100.0) ) ) / 100.0;
        sparcelas = String.valueOf(parcelas);

        //substituindo o "." por ","
        sparcelas = sparcelas.replace(".", ",");

        ftxtVlrParcelas.setText(sparcelas);
    }

    private void calcular_taxas() throws ParseException {
        //buscando os textos das caixas de texto
        sinicial = ftxtVlrInicial.getText();
        sparcelas = ftxtVlrParcelas.getText();
        meses = Integer.parseInt(ftxtMeses.getText());

        //Substituindo o texto recebido por valor numerico
        inicial = NumberFormat.getInstance().parse(sinicial).doubleValue();
        parcelas = NumberFormat.getInstance().parse(sparcelas).doubleValue();
        total = parcelas * meses;

        descobrir_taxa();

        staxaFinal = String.valueOf(taxaFinal);

        //substituindo o "." por ","
        staxaFinal = staxaFinal.replace(".", ",");

        ftxtTxJuros.setText(staxaFinal);
    }

    private void calcular_meses() throws ParseException {
        //buscando os textos das caixas de texto
        sinicial = ftxtVlrInicial.getText();
        sparcelas = ftxtVlrParcelas.getText();
        staxa = ftxtTxJuros.getText();

        //Substituindo o texto recebido por valor numerico
        inicial = NumberFormat.getInstance().parse(sinicial).doubleValue();
        parcelas = NumberFormat.getInstance().parse(sparcelas).doubleValue();
        taxa = NumberFormat.getInstance().parse(staxa).doubleValue();

        descobrir_meses();

        ftxtMeses.setText(String.valueOf(dmeses));
    }

    private void descobrir_taxa() {
        if (total>inicial){
            double i=0.00001;
            double inicialTeste=total;

            dmeses = meses;
            inicial = ( Math.round( (inicial * 100.0) ) ) / 100.0;

            while (inicialTeste>inicial){
                //efetuando o calculo conforme a formula:
                //inicial = (1-(1+juros)^(-meses))*parcelas/juros
                base = 1 + i;
                dmeses = - meses;
                potencia = Math.pow(base, dmeses);
                inicialTeste = (1-potencia) * parcelas/i;
                inicialTeste = ( Math.round( (inicialTeste * 100000.0) ) ) / 100000.0;
                
                if ((inicialTeste == inicial)||(inicialTeste<inicial)){
                    taxaFinal = i;
                    //Formatando a saida da variavel taxa para caixa de texto Taxa de Juros.
                    //Ex: de 0,0250810 para 2,50810 (%)
                    taxaFinal = ( Math.round( (taxaFinal * 10000000.0) ) ) / 100000.0;
                }
                i = i + 0.00000001;
            }
        }
        if (total<inicial){
            JOptionPane.showMessageDialog(null, "O valor Final está resultando inferior ao valor Inicial");
        }
        if (total == inicial){
            taxaFinal = 0.0;
        }

    }

    private void descobrir_meses() {
        double parcelaTeste=parcelas+1;
        meses = 1;
        inicial = ( Math.round( (inicial * 100.0) ) ) / 100.0;
        taxa = taxa/100;

        while (parcelaTeste>parcelas){
            //efetuando o calculo conforme a formula:
            // parcelas = inicial * juros/(1-(1+juros)^(-meses))
            base = 1 + taxa;
            dmeses = - meses;
            potencia = Math.pow(base, dmeses);
            parcelaTeste = inicial * taxa / (1-potencia);
            parcelaTeste = ( Math.round( (parcelaTeste * 100.0) ) ) / 100.0;

            if ((parcelaTeste == parcelas)||(parcelaTeste<parcelas)){
                dmeses = meses;
            }
            meses = meses + 1;
        }
    }

    public Boolean validaDados(){
        Boolean retorno = true;
        String erros = new String();

        if (ftxtVlrInicial.getText().equals("")){
            erros += "Valor Inicial Não Informado\n";
            retorno = false;
        }

        if (ftxtVlrParcelas.getText().equals("")){
            erros += "Valor da Parcela Não Informado\n";
            retorno = false;
        }

        if (ftxtTxJuros.getText().equals("")){
            erros += "Valor do Juros Não Informado\n";
            retorno = false;
        }

        if (ftxtMeses.getText().equals("")){
            erros += "Meses Não Informado\n";
            retorno = false;
        }

        if (!retorno){
            JOptionPane.showMessageDialog(null, "Os seguinte(s) erro(s) ocorreu(ram):\n"+erros);
        }

        return retorno;
    }

    private void limpar_dados() {
        //limpando os objetos da tela
        ftxtVlrInicial.setText("");
        ftxtVlrParcelas.setText("");
        ftxtTxJuros.setText("");
        ftxtMeses.setText("");

        //limpando as variaveis do sistema
        sinicial = "";
        sparcelas = "";
        staxa = "";
        staxaFinal = "";
        potencia = 0.0;
        base = 0.0;
        inicial = 0.0;
        parcelas = 0.0;
        taxa = 0.0;
        taxaFinal = 0.0;
        dmeses = 0.0;
        total = 0.0;
        meses = 0;
        selecao = 0;
    }
}
